<template>

    <div class="console" id="terminal"></div>


</template>
<script>
    import Terminal from './Xterm'
    export default {
        name: 'Console',
        props: {
            ipaddr: "",    //父组件传值给子组件
            terminal: {
                type: Object,
                default: {}
            }
        },
        data () {
            return {
                term: null,
                terminalSocket: null
            }
        },
        methods: {
            runRealTerminal () {
                console.log('webSocket is finished')
            },
            errorRealTerminal () {
                console.log('error')
            },
            closeRealTerminal () {
                console.log('close')
            }
        },
        mounted () {
            console.log('pid : ' + this.terminal.pid + ' is on ready')
            let terminalContainer = document.getElementById('terminal')
            this.term = new Terminal()
            this.term.open(terminalContainer)
            this.term.writeln('Warning: Note security, all your actions will be recorded !!! ')

            //窗口调整
            //let height = document.body.clientHeight;
           // let rows = height;
           // alert(rows)
            this.term.fit()  //窗口自适应
            this.term.scrollToBottom();
            //this.term.resize(this.term.cols,rows)
            this.term.resize(this.term.cols,46)   //在浏览器中调试出来的，不知道怎么
            //myself

            // open websocket
            this.terminalSocket = new WebSocket('ws://192.168.138.145:3000/terminals?' + "ip=" + this.ipaddr)   //传递参数
            this.terminalSocket.onopen = this.runRealTerminal
            this.terminalSocket.onclose = this.closeRealTerminal
            this.terminalSocket.onerror = this.errorRealTerminal
            this.term.attach(this.terminalSocket)
            this.term._initialized = true
            console.log('mounted is going on')
        },
        beforeDestroy () {
            this.terminalSocket.close()
            this.term.destroy()
        }
    }
</script>